//
//  WelcomeBackView.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/25.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeBackView: UIView {

    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        
       backgroundColor = UIColor(patternImage: UIImage(named:"ad_background")!)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
       // print("1")
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-180)
        }
      
        UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //会自动根据自动布局来实现动画
            self.layoutIfNeeded()
            }) { (Bool) -> Void in
            self.removeFromSuperview()
        }

    }
    //搞定方法的生命周期
    override func willMoveToWindow(newWindow: UIWindow?) {
       // print("2")
    }
    
    
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(self)
       // print("3")
        
    }
    
//    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//        removeFromSuperview()
//    }
    
    private lazy var iconView = UIImageView(image: UIImage(named:"avatar_default_big"))
   // private lazy var backView = UIImageView(image:UIImage(named:"ad_background"))
    
    private lazy var textLabel = UILabel(cztitle:"欢迎归来", color:UIColor.darkGrayColor(), fontSize:20.0, alignment: .Center)
}

extension WelcomeBackView{
    
    private func setupUI(){
        
        
        //   addSubview(backView)
        addSubview(iconView)
        addSubview(textLabel)
        
        let iconWth = 45
        iconView.layer.cornerRadius = CGFloat(iconWth)
        iconView.layer.masksToBounds = true
        
        
        //自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(50)
            make.size.equalTo(CGSize(width: iconWth * 2, height: iconWth * 2))
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.centerX.equalTo(iconView)
        }
        
        //不强行更新约束的话，界面的动画效果  试试
        layoutIfNeeded()
        
        //这里 ！ 不加的话也可以，但是调用下面的方法，url必须要有值
        //        let url = NSURL(string: UserAccount.shardUserAccount.avatar_large ?? "")!
        //        iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named:"avatar_default_big"))
//        iconView.cz_setImage(UserAccount.shardUserAccount.avatar_large, CZPlaceholderImage:"avatar_default_big")
        iconView.cz_setImage(UserAccount.shardUserAccount.avatar_large, CZPlaceholderImage:"avatar_default_big",size:CGSize(width: iconWth * 2, height: iconWth * 2))
        
    }
    
    
    
}
